package com.example.controller;

import com.example.modal.SysUser;
import com.example.pojo.RestfulApiResponse;
import com.example.service.SysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author jie
 * @date 2021/11/18 -10:13
 */
@Api("用户")
@RestController
public class SysUserController {
    @Autowired
    private SysUserService sysUserService;

    @ApiOperation("insert")
    @GetMapping("insert")
    public int insert(String name) {
        if (StringUtils.hasLength(name)) {
            throw new RuntimeException("这个是一个异常");
        }
        SysUser user = new SysUser();
        user.setName(name);
        return sysUserService.insert(user);
    }

    @ApiOperation("get")
    @GetMapping("get")
    public SysUser get(String id) {
        return sysUserService.getById(id);
    }

    @ApiOperation("getAll")
    @GetMapping("getall")
    public RestfulApiResponse<List<SysUser>> getAll() {
        return RestfulApiResponse.success(sysUserService.fetchAll());
    }

    @ApiOperation("update")
    @GetMapping("update")
    public boolean update(SysUser user) {
        return sysUserService.updateById(user);
    }

    @ApiOperation("delete")
    @GetMapping("delete")
    public boolean delete(String id) {
        return sysUserService.removeById(id);
    }
}
